package cacy.leetcode.array;


import java.util.HashSet;
import java.util.Set;

/**
 * @author 凯齐
 * @date 2025-02-21 09:41
 * @description
 */

class SolutionArray04A {
    public int findRepeatDocument(int[] documents) {
        int[] exist = new int[documents.length];
        exist[0] = documents[0];
        int res = -1, existNovel = 1, flag = 0;
        for (int i = 1; i < documents.length; i++) {
            for (int j = 0; j < existNovel; j++) {
                if (documents[i] == exist[j]) {
                    res = documents[i];
                    flag = 1;
                    break;
                }
            }
            if (flag == 1) {
                break;
            }else {
                exist[existNovel] = documents[i];
                existNovel++;
                flag = 0;
            }
        }
        return res;
    }
}

class SolutionArray04B {
    public int findRepeatDocument(int[] documents) {
        Set<Integer> hmap = new HashSet<>();
        for(int doc : documents) {
            if(hmap.contains(doc)) {
                return doc;
            }
            hmap.add(doc);
        }
        return -1;
    }
}

public class Array04 {
    public static void main(String[] args) {
        // 测试数组
        int[] documents = {1,2,3,4};
        int res = new SolutionArray04A().findRepeatDocument(documents);
        System.out.println(res);
    }
}
